package com.gs.synedocket.presenter;

import com.google.gson.reflect.TypeToken;
import com.gs.synedocket.apiresult.ApiResultData;
import com.gs.synedocket.constant.AllConstants;
import com.gs.synedocket.db.dto.DocketDto;
import com.gs.synedocket.model.DocketModel;
import com.gs.synedocket.model.listener.OnResultListener;
import com.gs.synedocket.util.SPUtil;
import com.gs.synedocket.view.IDocketView;

import java.lang.reflect.Type;

public class DocketPresenter extends BasePresenter<IDocketView> {

    private DocketModel docketModel = new DocketModel();

    public void getData(String docketId) {
        if (getView() != null) {
            Type typeofT = new TypeToken<ApiResultData<DocketDto>>() {
            }.getType();
            docketModel.getOn(docketId, SPUtil.getInstance().getString(AllConstants.TRUCK_NAME),new OnResultListener() {
                @Override
                public void onSuccess(Object aipResult) {
                    if (getView() != null) {
                        getView().hideLoading();
                        getView().onGetDataSuccess((ApiResultData<DocketDto>) aipResult);
                    }
                }

                @Override
                public void onFails(Exception e) {
                    if (getView() != null) {
                        getView().hideLoading();
                        getView().onGetDataFail(e);
                    }
                }
            }, typeofT);
        }
    }

    public void getScanPaper(String docketNo) {
        if (getView() != null) {
            getView().showLoading();
            Type typeofT = new TypeToken<ApiResultData<DocketDto>>() {
            }.getType();
            docketModel.scanPaper(docketNo,new OnResultListener() {
                @Override
                public void onSuccess(Object aipResult) {
                    if (getView() != null) {
                        getView().hideLoading();
                        getView().onGetDataSuccess((ApiResultData<DocketDto>) aipResult);
                    }
                }

                @Override
                public void onFails(Exception e) {
                    if (getView() != null) {
                        getView().hideLoading();
                        getView().onGetDataFail(e);
                    }
                }
            }, typeofT);
        }
    }
}
